package com.example.com.skills_utf8;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

public class _51Activity extends Activity {

	

	private int scroolTime = 50;
	private int showContent = 0;
	private TextView tv_content;

	private int currentIndex = 0;
	private boolean haveNotice = true;
	private List<Notice> noticeList = new ArrayList();
	
	
	////
	TextView tv_content_horizontal;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_51);

		tv_content = (TextView) findViewById(R.id.tv_content);

		for (int i = 0; i < 20; i++) {

			Notice localNotice = new Notice();
			localNotice.text = ("hello world: " + i);
			noticeList.add(localNotice);
		}

		hyhandler.sendEmptyMessage(1);
		
		
		
		
		//////////
		tv_content_horizontal = (TextView)findViewById(R.id.tv_content_horizontal);
		tv_content_horizontal.setText("少年三国志");
		//handler_horizontal.sendEmptyMessage(1);
		
		content_tittle = tv_content_horizontal.getText().toString();
		
		// 放置文字
		for(int i =0 ; i<tv_content_horizontal.getWidth(); i++){
			content_tittle +=" ";
		}
		content_tittle += content_tittle;
		
		tv_content_horizontal.setText(content_tittle);
		
		handler_horizontal();
		

	}


	private Handler hyhandler = new Handler() {
		public void handleMessage(Message paramAnonymousMessage) {
			if (paramAnonymousMessage.what == 1) {
				handler();
			}
		}
	};

	// 一个任务
	Runnable runnable = new Runnable() {
		public void run() {
			int height = tv_content.getLineHeight();
			int j = tv_content.getLineCount() / 2;

			showContent++;
			tv_content.scrollBy(0, 1);  // 每次滚动1像素
			hyhandler.postDelayed(this, scroolTime);
			if (showContent >= j * height) {
				handler();
				
				// 使变量复原
				tv_content.scrollBy(0, -1 * (j * height));
				showContent = 0;
			}
		}
	};

	public void handler() {
		hyhandler.removeCallbacks(runnable);
		currentIndex %= noticeList.size();   // 牛逼; 这样就限定住了循环 
		tv_content.setText(((Notice) noticeList.get(currentIndex)).text + "\n"
				+ ((Notice) noticeList.get((1 + currentIndex) % noticeList.size())).text);
		
		currentIndex++;
		
		if (haveNotice) {
			hyhandler.postDelayed(runnable, scroolTime);
		}
	}

	class Notice {
		String text;

	}
	
	////////////////////////////////////////////////////////////////横向滚动
	
	
	Timer mTimer = new Timer();
	
	int showContent_horizontal = 0;
	String content_tittle = "";
	
	
	/*
	TimerTask runnable_horizontal = new TimerTask() {
		public void run() {
			int height = tv_content_horizontal.getLineHeight();
			int width = tv_content_horizontal.getWidth();

			
			for(;showContent_horizontal<width; showContent_horizontal++){
				
				try {
					Thread.sleep(33);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				tv_content_horizontal.scrollBy(1, 0);  // 每次滚动1像素
			}
			
			
			//if (showContent_horizontal >= width) 
			{
				handler_horizontal();
				
				// 使变量复原
				tv_content_horizontal.scrollBy(-width,0);
				showContent_horizontal = 0;
			}
			
			
		}
	};
	*/
	
	
	class TimerTaskHorizontal extends TimerTask
	{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int height = tv_content_horizontal.getLineHeight();
			int width = tv_content_horizontal.getWidth();

			
			for(;showContent_horizontal<width; showContent_horizontal++){
				
				
				try {
					Thread.sleep(33);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				tv_content_horizontal.scrollBy(1, 0);  // 每次滚动1像素
			}
			
			
			//if (showContent_horizontal >= width) 
			{
				handler_horizontal();
				
				// 使变量复原
				tv_content_horizontal.scrollBy(-width,0);
				showContent_horizontal = 0;
			}
			
		}
		
	};
	
	
	
	public void handler_horizontal() {	
		

		
		if (haveNotice) {
			
			mTimer = new Timer();
			mTimer.schedule(new TimerTaskHorizontal(), scroolTime);
		}
	}

	
	///////////////////////////////////////////////////////////////

	

}



